package com.ruoyi.labdata.controller;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.course.domain.LabCourseTimetable;
import com.ruoyi.course.service.ILabCourseTimetableService;
import com.ruoyi.labdata.domain.LabHolidayReservation;
import com.ruoyi.labdata.domain.LabOrgnization;
import com.ruoyi.labdata.service.ILabHolidayReservationService;
import com.ruoyi.labdata.service.ILabOrgnizationService;

/**
 * 实验室是否可预约Controller
 * 
 * @author jun.wang
 * @date 2022-04-15
 */
@RestController
@RequestMapping("/labdata/labReservation")
public class LabHolidayReservationController extends BaseController {
	@Autowired
	private ILabHolidayReservationService labHolidayReservationService;
	@Autowired
	private ILabCourseTimetableService labCourseTimetableService;
	@Autowired
	private ILabOrgnizationService labOrgnizationService;
	/**
	 * 查询实验室是否可预约列表
	 */
	@PreAuthorize("@ss.hasPermi('labdata:labReservation:query')")
	@GetMapping("/list")
	public TableDataInfo list(LabHolidayReservation labHolidayReservation) {
		
		
		startPage();
		List<LabHolidayReservation> list = labHolidayReservationService
				.selectLabHolidayReservationList(labHolidayReservation);
		if(list.size()==0){
			LabOrgnization labOrgnization = labOrgnizationService.selectLabOrgnizationById(labHolidayReservation.getClassRoomId());
				if(labOrgnization.getOrgFunctionOpt()==4){
				//查询是否存在当前学期的预定日期
				LabCourseTimetable timetable = labCourseTimetableService.getDefaultTimetable(new Date());
				if(timetable==null){
					throw new RuntimeException("本学期的日历数据尚未录入，请先录入学期日历数据。");
				}
				//没有的话自动生成
				labHolidayReservationService.generalCalendarDate(timetable, labHolidayReservation.getClassRoomId());
				list = labHolidayReservationService
						.selectLabHolidayReservationList(labHolidayReservation);
			}
		}
		return getDataTable(list);
	}

	/**
	 * 导出实验室是否可预约列表
	 */
	// @PreAuthorize("@ss.hasPermi('labdata:labReservation:export')")
	/*
	 * @Log(title = "实验室是否可预约", businessType = BusinessType.EXPORT)
	 * 
	 * @GetMapping("/export") public AjaxResult export(LabHolidayReservation
	 * labHolidayReservation) { List<LabHolidayReservation> list =
	 * labHolidayReservationService.selectLabHolidayReservationList(
	 * labHolidayReservation); ExcelUtil<LabHolidayReservation> util = new
	 * ExcelUtil<LabHolidayReservation>(LabHolidayReservation.class); return
	 * util.exportExcel(list, "labReservation"); }
	 */

	/**
	 * 获取实验室是否可预约详细信息
	 */
	@PreAuthorize("@ss.hasPermi('labdata:labReservation:query')")
	@GetMapping(value = "/{dayId}")
	public AjaxResult getInfo(@PathVariable("dayId") Long dayId) {
		return AjaxResult.success(labHolidayReservationService.selectLabHolidayReservationById(dayId));
	}

	/**
	 * 新增实验室是否可预约
	 */
	@PreAuthorize("@ss.hasPermi('labdata:labReservation:edit')")
	@Log(title = "实验室是否可预约", businessType = BusinessType.INSERT)
	@PostMapping
	public AjaxResult add(@RequestBody LabHolidayReservation labHolidayReservation) {
		return toAjax(labHolidayReservationService.insertLabHolidayReservation(labHolidayReservation));
	}

	/**
	 * 修改实验室是否可预约
	 */
	@PreAuthorize("@ss.hasPermi('labdata:labReservation:edit')")
	@Log(title = "实验室是否可预约", businessType = BusinessType.UPDATE)
	@PutMapping
	public AjaxResult edit(@RequestBody LabHolidayReservation labHolidayReservation) {
		return toAjax(labHolidayReservationService.updateLabHolidayReservation(labHolidayReservation));
	}

	/**
	 * 删除实验室是否可预约
	 */
	// @PreAuthorize("@ss.hasPermi('labdata:labReservation:remove')")
	/*
	 * @Log(title = "实验室是否可预约", businessType = BusinessType.DELETE)
	 * 
	 * @DeleteMapping("/{dayIds}") public AjaxResult remove(@PathVariable Long[]
	 * dayIds) { return
	 * toAjax(labHolidayReservationService.deleteLabHolidayReservationByIds(
	 * dayIds)); }
	 */

	/**
	 * 查询实验室预约统计
	 */
	@PreAuthorize("@ss.hasPermi('labdata:labReservation:statistic')")
	@GetMapping("/listLabReservationStatistics")
	public TableDataInfo listLabReservationStatistics(LabHolidayReservation labHolidayReservation) {
		return labHolidayReservationService.listLabReservationStatistics(labHolidayReservation);
	}
}
